package com.wust.demo1.action;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.wust.demo1.module.User;
import com.wust.demo1.service.UserService;
import com.wust.demo1.util.EncrypMD5;
import com.wust.demo1.util.HtmlUtil;
import com.wust.demo1.util.RandomUtil;
import com.wust.demo1.util.SimpleMailSender;

public class UserAction {
	private UserService userService;
	private User user;
	private List<User> users;
	private int rows;
	private int page;
	
	public String toLogin(){
		return "success";
	}
	public String toReg(){
		return "success";
	}
	
	public String login(){
		if(user != null){
			if(this.getUserService().isRightPassword(user)){//如果密码正确
				return "success";
			}
		}
		return "failure";
	}
	public String reg(){
		if(user != null){
			try {
				String md5Password = EncrypMD5.eccrypt(user.getUserPassword());
				String md5Email = EncrypMD5.eccrypt(user.getUserEmail());
				user.setUserPassword(md5Password);
				user.setUserEmail(md5Email);
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.getUserService().saveUser(user);
		}
		return "success";
	}
	public String toAddUser(){
		return "success";
	}
	public void addUser(){
		if(user != null){
			try {
				String md5Password = EncrypMD5.eccrypt(user.getUserPassword());
				String md5Email = EncrypMD5.eccrypt(user.getUserEmail());
				user.setUserPassword(md5Password);
				user.setUserEmail(md5Email);
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.getUserService().saveUser(user);	
		}
		HtmlUtil.returnMessage("save success!");
	}
	
	public void compareEmail(){
		User tempUser = new User();
		tempUser = this.getUser();
		Boolean isCompare = this.getUserService().isRightEmail(tempUser);
		HttpServletResponse response = ServletActionContext.getResponse();
		try {
			response.getWriter().write(isCompare.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				response.getWriter().close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	public String toFindPassword(){                        //找回密码
		String checkUserEmail = this.getUser().getUserEmail();
		if(checkUserEmail != null){
			String md5CheckUserEmail;
			try {
				md5CheckUserEmail = EncrypMD5.eccrypt(checkUserEmail);
				User tempUser = new User();
				tempUser = this.getUserService().getUserByEmail(md5CheckUserEmail);
				if(tempUser != null){
					String newPassword = RandomUtil.generateString(6);
					String md5NewPassword = EncrypMD5.eccrypt(newPassword);
					tempUser.setUserPassword(md5NewPassword);
					this.getUserService().updateUserPassword(tempUser);
					SimpleMailSender.sendEmail(newPassword, checkUserEmail);
				}else{
					return "faliure";
				}
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch blocks
				e.printStackTrace();
			}
		}else{
			return "faliure";
		}
		return "success";
	}
	
	//查询所有用户
	public void findAllUser(){
		System.out.println("this.getPage()"+this.getPage());
		System.out.println("this.getRows()"+this.getRows());
		users = this.getUserService().getAllUsers();
		JSONArray jsonArray = new JSONArray();
		for(int i = 0; i<users.size(); i++){
			User u = (User)users.get(i);
			JSONObject jsonObjectTemp = new JSONObject();
			jsonObjectTemp.put("userName", u.getUserName());
			jsonObjectTemp.put("userEmail", u.getUserEmail());
			jsonObjectTemp.put("id", u.getId());
			jsonArray.add(jsonObjectTemp);
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("total", 239);
		jsonObject.put("rows", jsonArray.toString());
		HtmlUtil.returnMessage(jsonObject.toString());
	}
	
	public void deleteUserById(){
		Long tempId = this.getUser().getId();
		if(tempId != null){
			this.getUserService().deleteUserById(tempId);
		}
		HtmlUtil.returnMessage("success");
	}
	public String toFindAllUser(){
		return "success";
	}
	public String findPassword(){
		
		return "success";
	}
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	public List<User> getUsers() {
		return users;
	}
	public void setUsers(List<User> users) {
		this.users = users;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
}
